<template>
  <div class="p-[20px] bg-white">
    <el-steps
      class="w-[800px] mb-[40px]"
      :active="active"
      finish-status="success"
      align-center
    >
      <el-step title="创建" />
      <el-step title="创建成功" />
    </el-steps>
    <el-form
      class="w-[800px]"
      label-width="200"
      :rules="rules"
      :model="form"
      ref="formRef"
    >
      <el-form-item label="分账接收方名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="联系手机号" prop="contactMobile">
        <el-input v-model="form.contactMobile" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="收款账户类型" prop="acctTypeCode">
        <el-radio-group v-model="form.acctTypeCode">
          <el-radio label="57">对公</el-radio>
          <el-radio label="58">对私</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="收款账户卡号" prop="acctNo">
        <el-input v-model="form.acctNo" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="收款账户名称" prop="acctName">
        <el-input v-model="form.acctName" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="收款账户证件类型" prop="acctCertificateType">
        <el-select v-model="form.acctCertificateType" placeholder="请选择">
          <el-option
            v-for="item in CERTIFICATE_TYPE"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="收款账户证件号" prop="acctCertificateNo">
        <el-input v-model="form.acctCertificateNo" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="收款账户开户名称" prop="acctOpenBankName">
        <el-input v-model="form.acctOpenBankName" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="收款账户开户行号" prop="acctOpenBankCode">
        <el-input v-model="form.acctOpenBankCode" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="收款账户清算行行号" prop="acctClearBankCode">
        <el-input v-model="form.acctClearBankCode" placeholder="请输入" />
      </el-form-item>
      <template v-if="form.acctTypeCode === '57'">
        <el-form-item
          label="营业执照号码"
          prop="licenseNo"
          :rules="[{ required: true, message: '请输入' }]"
        >
          <el-input v-model="form.licenseNo" placeholder="请输入" />
        </el-form-item>
        <el-form-item
          label="营业执照名称"
          prop="licenseName"
          :rules="[{ required: true, message: '请输入' }]"
        >
          <el-input v-model="form.licenseName" placeholder="请输入" />
        </el-form-item>
        <el-form-item
          label="法人姓名"
          prop="legalPersonName"
          :rules="[{ required: true, message: '请输入' }]"
        >
          <el-input v-model="form.legalPersonName" placeholder="请输入" />
        </el-form-item>
        <el-form-item
          label="法人证件类型"
          prop="legalPersonCertificateType"
          :rules="[{ required: true, message: '请选择' }]"
        >
          <el-select
            v-model="form.legalPersonCertificateType"
            placeholder="请选择"
          >
            <el-option
              v-for="item in CERTIFICATE_TYPE"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="法人证件号"
          prop="legalPersonCertificateNo"
          :rules="[{ required: true, message: '请输入' }]"
        >
          <el-input
            v-model="form.legalPersonCertificateNo"
            placeholder="请输入"
          />
        </el-form-item>
      </template>
      <el-form-item label="法人身份证正面(国徽面)" prop="idFrontPic">
        <SingleUpload
          v-model="form.idFrontPic"
          :type="UploadEnum.FR_ID_CARD_FRONT"
          :extra="{ merCupNo: $route.query.merCupNo }"
          @success="
            (data) => {
              form.idFrontPicAttFileId = data.attFileId;
              form.idFrontPicFileName = data.fileName;
            }
          "
        />
        <div class="form-helper block w-full">支持5M以内jpg/png/pdf格式</div>
      </el-form-item>
      <el-form-item label="法人身份证反面(人像面)" prop="idBehindPic">
        <SingleUpload
          v-model="form.idBehindPic"
          :type="UploadEnum.FR_ID_CARD_BEHIND"
          :extra="{ merCupNo: $route.query.merCupNo }"
          @success="
            (data) => {
              form.idBehindPicAttFileId = data.attFileId;
              form.idBehindPicFileName = data.fileName;
            }
          "
        />
        <div class="form-helper block w-full">支持5M以内jpg/png/pdf格式</div>
      </el-form-item>
      <el-form-item label="银行卡" prop="bankCardPic">
        <SingleUpload
          v-model="form.bankCardPic"
          :type="UploadEnum.BANK_CARD"
          :extra="{ merCupNo: $route.query.merCupNo }"
          @success="
            (data) => {
              form.bankCardPicAttFileId = data.attFileId;
              form.bankCardPicFileName = data.fileName;
            }
          "
        />
        <div class="form-helper block w-full">支持5M以内jpg/png/pdf格式</div>
      </el-form-item>
      <template v-if="form.acctTypeCode === '57'">
        <el-form-item label="营业执照" prop="businessLicencePic">
          <SingleUpload
            v-model="form.businessLicencePic"
            :type="UploadEnum.BUSINESS_LICENCE"
            :extra="{ merCupNo: $route.query.merCupNo }"
            @success="
              (data) => {
                form.businessLicencePicAttFileId = data.attFileId;
                form.businessLicencePicFileName = data.fileName;
              }
            "
          />
          <div class="form-helper block w-full">支持5M以内jpg/png/pdf格式</div>
        </el-form-item>
      </template>
      <el-form-item>
        <el-button type="primary" @click="handleSubmit">保存</el-button>
        <el-button type="primary" @click="() => $router.go(-1)">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import PaymentSplitAPI, { ReceiverVO } from "@/api/split";
import { UploadEnum } from "@/enums/UploadEnum";
import { onMounted, ref } from "vue";
import { useRoute } from "vue-router";

const form = ref<ReceiverVO>({});
const active = ref(0);

const CERTIFICATE_TYPE = [
  { label: "身份证", value: "17" },
  { label: "护照", value: "18" },
  { label: "港澳通行证", value: "19" },
  { label: "台胞证", value: "20" },
];

const rules = {
  name: [{ required: true, message: "请输入" }],
  contactMobile: [{ required: true, message: "请输入" }],
  acctName: [{ required: true, message: "请输入" }],
  acctNo: [{ required: true, message: "请输入" }],
  acctCertificateType: [{ required: true, message: "请选择" }],
  acctCertificateNo: [{ required: true, message: "请输入" }],
  acctOpenBankName: [{ required: true, message: "请输入" }],
  acctOpenBankCode: [{ required: true, message: "请输入" }],
  acctClearBankCode: [{ required: true, message: "请输入" }],
  idFrontPic: [{ required: true, message: "请上传" }],
  idBehindPic: [{ required: true, message: "请上传" }],
  bankCardPic: [{ required: true, message: "请上传" }],
};

const formRef = ref();

const handleSubmit = () => {
  formRef.value.validate((valid: boolean) => {
    if (valid) {
      const { id } = form.value;
      const submitData = { ...form.value, storeCode: route.query.storeCode };
      if (id) {
        PaymentSplitAPI.updateReceiver(submitData).then((res) => {
          ElMessage.success("修改成功");
        });
      } else {
        PaymentSplitAPI.addReceiver(submitData).then((res) => {
          ElMessage.success("添加成功");
        });
      }
    }
  });
};

const route = useRoute();

onMounted(() => {
  if (route.query.id) {
    PaymentSplitAPI.getReceiverDetail(route.query.id as string).then((res) => {
      form.value = res ?? {};
    });
  }
});
</script>
